﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccess;
using SubSonic;

namespace Business
{
    public class GridContractType
    {
        private  string _VillageName;
        public  string VillageName
        {
            get { return _VillageName; }
            set { _VillageName = value; }
        }

        private string _VillageCode;
        public string VillageCode
        {
            get { return _VillageCode; }
            set { _VillageCode = value; }
        }

        private int _Khanevar;
        public int Khanevar
        {
            get { return _Khanevar; }
            set { _Khanevar = value; }
        }

        private int _Population;
        public int Population
        {
            get { return _Population; }
            set { _Population = value; }
        }


        private  string _AdvisorName;
        public  string AdvisorName
        {
            get { return _AdvisorName; }
            set { _AdvisorName = value; }
        }

        private  string _ContractDate;
        public  string ContractDate
        {
            get { return _ContractDate; }
            set { _ContractDate = value; }
        }
        /// <summary>
        /// For calculating progress of each project.
        /// </summary>
        private  int _Progress;
        public  int Progress
        {
            get
            {
                _Progress = CommonCalculations.ProgressCalculator(ContractId);
                return _Progress;
            }
            set
            {
                _Progress = value;
            }
        }

        private string _ContractNumber;
        public string ContractNumber
        {
            get { return _ContractNumber; }
            set { _ContractNumber = value; }
        }

        private Guid _ContractId;
        public Guid ContractId
        {
            get { return _ContractId; }
            set { _ContractId = value; }
        }

        private int _Delay;
        public int Delay
        {
            get
            {
                return _Delay ;
            }
            set
            {
                _Delay = value;
            }
        }

        private bool _HasDelay;
        public bool HasDelay
        {
            get { return _HasDelay; }
            set { _HasDelay = value; }
        }


        private string _HasDelayText;
        public string HasDelayText
        {
            get { return _HasDelayText; }
            set { _HasDelayText = value; }
        }

        private bool _ContractType;
        public bool ContractType
        {
            get { return _ContractType; }
            set { _ContractType = value; }
        }

        private string _ContractTypeText;
        public string ContractTypeText
        {
            get { return _ContractTypeText; }
            set { _ContractTypeText = value; }
        }

        private string _Dehestan;
        public string Dehestan
        {
            get { return _Dehestan; }
            set
            {
                _Dehestan = value;
            }
        }

        private string _Bakhsh;
        public string Bakhsh
        {
            get { return _Bakhsh; }
            set
            {
                _Bakhsh = value;
            }
        }

        private string _City;
        public string City
        {
            get { return _City; }
            set
            {
                _City = value;
            }
        }

        private int _StepsCount;
        public int StepsCount
        {
            get { return _StepsCount; }
            set
            {
                _StepsCount = value;
            }
        }

        private string _AdvisorFullName;
        public string AdvisorFullName
        {
            get { return _AdvisorFullName; }
            set
            {
                _AdvisorFullName = value;
            }
        }

        private string _ContractYear;
        public string ContractYear
        {
            get { return _ContractYear; }
            set
            {
                _ContractYear = value;
            }
        }
        

        /// <summary>
        /// Second constructor returns details
        /// </summary>
        /// <param name="villageId"></param>
        /// <param name="advisorId"></param>
        /// <param name="contractDate"></param>
        /// <param name="contractNumber"></param>
        /// <param name="contractId"></param>
        public GridContractType(Guid villageId, Guid advisorId, DateTime contractDate, string contractNumber, Guid contractId, bool detailsNeeded, bool contractType)
        {
            var villageDetails = DataAccess.TblVillage.FetchByID(villageId);
            VillageName = villageDetails.Name;
            Khanevar = Convert.ToInt32(villageDetails.Khanevar);
            Population = Convert.ToInt32(villageDetails.Population);
            ContractDate = PersianDateControls.Convertor.ToPersianDate(contractDate).ToString();
            AdvisorName = DataAccess.AspnetUser.FetchByID(advisorId).UserName;
            ContractNumber = contractNumber;
            ContractId = contractId;
            ContractType = contractType;
            ContractTypeText = CommonCalculations.ContractTypeNameString(ContractType);

            if (detailsNeeded)
            {
                Delay = CommonCalculations.DelayCalculator( contractId );
                Progress = CommonCalculations.ProgressCalculator( contractId );
            }
            else
            {
                HasDelay = CommonCalculations.HasDelayCalculator(contractId);
                HasDelayText = HasDelay == true ? "دارد" : " ندارد";
            }
            TblDehestan dehestan = TblDehestan.FetchByID(villageDetails.DehstanId);
            TblBakhsh bakhsh = TblBakhsh.FetchByID( dehestan.BakhshId );
            TblCity city = TblCity.FetchByID( bakhsh.CityId );
            Dehestan = dehestan.Name;
            Bakhsh = bakhsh.Name;
            City = city.Name;
            VillageCode = villageDetails.VillageCode;
            StepsCount = Business.CommonCalculations.StepsCount(ContractId);
            AdvisorFullName = CommonCalculations.FindDoerOfProject(ContractId);
            ContractYear = Convert.ToString(TblContract.FetchByID(ContractId).Year);
        }
    }
}
